/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormTeste.java
 *
 * Created on 03/11/2011, 23:21:41
 */
package javalee.forms;

import com.sun.rowset.CachedRowSetImpl;
import java.sql.ResultSet;
import javalee.util.Calculos;
import javax.sql.rowset.CachedRowSet;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author ricardo
 */
public class FormTeste extends javax.swing.JFrame {

    /** Creates new form FormTeste */
    public FormTeste() {
        initComponents();
        calcTeste = new Calculos(); 
        Object[] colunas = { "ID Produto", "qnt1", "qnt2", "qnt3", "qnt4", "qnt5"};
        Object[][] linhas = {{""}};
        modelo = new DefaultTableModel(linhas, colunas);
        modeloProducao = new DefaultTableModel(linhas, colunas);
        modeloProducaoComposicao = new DefaultTableModel(linhas, colunas);
        modeloPedCompra = new DefaultTableModel(linhas, colunas);
        jTable1.setModel(modelo);
        jTable2.setModel(modeloProducao);
        jTable3.setModel(modeloProducaoComposicao);
        jTable4.setModel(modeloPedCompra);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTable3 = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTable4 = new javax.swing.JTable();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "idProd", "quant"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("Testar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "idProd", "quant"
            }
        ));
        jScrollPane2.setViewportView(jTable2);

        jLabel1.setText("Demanda Revista");

        jLabel2.setText("Producao recalculada");

        jTable3.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "idProd", "quant"
            }
        ));
        jScrollPane3.setViewportView(jTable3);

        jLabel3.setText("Composicao da producao");

        jTable4.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "idProd", "quant"
            }
        ));
        jScrollPane4.setViewportView(jTable4);

        jLabel4.setText("Pedido de compra");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jLabel3)
                        .addContainerGap(627, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 361, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(jLabel1))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 45, Short.MAX_VALUE)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jLabel2)
                                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 361, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                            .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 361, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(45, 45, 45)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jLabel4)
                                    .add(jScrollPane4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 361, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                        .addContainerGap())
                    .add(layout.createSequentialGroup()
                        .add(jButton1)
                        .addContainerGap(704, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 218, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel1))
                    .add(layout.createSequentialGroup()
                        .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 218, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel2)))
                .add(54, 54, 54)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 218, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 218, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel3)
                    .add(jLabel4))
                .add(49, 49, 49)
                .add(jButton1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        try
        {    
            CachedRowSet rsDemRev = new CachedRowSetImpl();
            rsDemRev.populate(calcTeste.demandaRevista());
             populaTable(jTable1, modelo, rsDemRev);
             
            CachedRowSet rsProd = new CachedRowSetImpl();
            rsProd.populate(calcTeste.producaoRecalc(rsDemRev));
            populaTable(jTable2, modeloProducao, rsProd );
            
            CachedRowSet rsProdComposicao = new CachedRowSetImpl();
            rsProdComposicao.populate(calcTeste.buscaComposicao(rsProd));
           populaCompEPedido(jTable3, jTable4, modeloProducaoComposicao, modeloPedCompra, rsProdComposicao);
        }
        catch (Exception e)
        {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
    }//GEN-LAST:event_jButton1ActionPerformed
    
    private void populaTable(JTable tabela, DefaultTableModel mod, ResultSet rs)
    {
        try {
            rs.first();
            do            
            {               
                tabela.setValueAt(rs.getInt("id_produto"), mod.getRowCount()-1, 0);
                for(int a = 1; a <= 5; a++)
                {
                    tabela.setValueAt(rs.getInt("qnt" + a), mod.getRowCount()-1, a);
                }
                mod.insertRow(mod.getRowCount(), new Object[] {""});
            }while(rs.next());
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Preenche tabela " + ex.getMessage());
        }
    }
    
    private void populaCompEPedido(JTable tabela, JTable tabela2, DefaultTableModel mod, DefaultTableModel mod2, ResultSet rs)
    {
        try {
            rs.first();
            do            
            {  
                if(rs.getString("origem").equals("M"))
                {
                    tabela2.setValueAt(rs.getInt("id_produto"), mod2.getRowCount()-1, 0);
                    for(int a = 1; a <= 5; a++)
                    {
                        tabela2.setValueAt(rs.getInt("qnt" + a), mod2.getRowCount()-1, a);
                    }
                    mod2.insertRow(mod2.getRowCount(), new Object[] {""});
                }
                else
                {
                    tabela.setValueAt(rs.getInt("id_produto"), mod.getRowCount()-1, 0);
                    for(int a = 1; a <= 5; a++)
                    {
                        tabela.setValueAt(rs.getInt("qnt" + a), mod.getRowCount()-1, a);
                    }
                    mod.insertRow(mod.getRowCount(), new Object[] {""});
                }
            }while(rs.next());
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Preenche tabela " + ex.getMessage());
        }
    }
    
    
    
   
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FormTeste.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FormTeste.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FormTeste.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FormTeste.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FormTeste().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTable jTable3;
    private javax.swing.JTable jTable4;
    // End of variables declaration//GEN-END:variables

    private Calculos calcTeste;
    private DefaultTableModel modelo;
    private DefaultTableModel modeloProducao;
    private DefaultTableModel modeloProducaoComposicao;
    private DefaultTableModel modeloPedCompra;
}
